*** PREP DATA

		*** load coded events  DATA
		import excel "$Replicationdirectory/_Data/News/ALL_EVENTS_coded.xlsx", sheet("v2") firstrow clear
		
		
				
		
	 
		
		
		*** drop individual codings  
				
				foreach x in Duplicates Excludedbecauseofcodingclash Excludeduplicatearticleont Excludedother{
 				
					drop if `x'==1
		
				}
 		
		 
		   
		 
		 
		foreach var in UK FR CH AM  {
		preserve
		keep if  NEWS_`var'==1  
		
		
		*** 
		
		
		
		collapse (mean) NEWS_POLITICAL NEWS_TRADE NEWS_FINANCIAL          (sum) shock (first) SOURCE ,by(date)
		 
		 
		 
		 
		 
		 		foreach x in 		 NEWS_POLITICAL NEWS_TRADE NEWS_FINANCIAL     {
				replace `x'= 1 if `x'>=0.1
				replace `x'= 0 if `x'<0.1 
			
				}
				
				 
				gen NEWS_positive=1 if shock>0
				gen NEWS_negative=1 if  shock<0
			 	gen NEWS_neutral=1 if shock==0
				 
				foreach y in positive negative {
					replace NEWS_`y'=0 if NEWS_`y'==.
				}
				
				keep SOURCE date   NEWS_POLITICAL NEWS_TRADE NEWS_FINANCIAL  shock  NEWS_positive NEWS_negative
 				 
				gen COUNTRY="`var'"
				
				save "$Replicationdirectory/_Work/TEMP/FTC_SHOCK_`var'", replace
				
				restore
				
		 		}

		  
		  
		 
		 clear
		 
		foreach var in UK FR CH AM{
		append using "$Replicationdirectory/_Work/TEMP/FTC_SHOCK_`var'"
		 
		} 
		
		
		 
		
		sort date
		by date: gen obs_per_event=_n
		sort obs_per_event date 
		
		gen ID_running_i=_n if obs_per_event==1
		
		egen ID_running=max(ID_running_i), by(date)
		
		drop ID_running_i
		  
 		   
		 
			gen market=""
			replace market="london" if COUNTRY=="UK" 
			replace market="paris" if COUNTRY=="FR"
			replace market="zurich" if COUNTRY=="CH"
			replace market="amsterdam" if COUNTRY=="AM"
			
			drop COUNTRY
			
			encode market, gen(market_enc)
			
			xtset market_enc date
			
			
 			
 			
			
			egen count_affected=count(ID_running) , by(date)
			
			drop if count_affected==0
			 
			
			 foreach x in 		 NEWS_POLITICAL NEWS_TRADE NEWS_FINANCIAL    shock NEWS_positive NEWS_negative  {
			
			replace `x'=0 if `x'==.

			 
			 }
			 
			 egen id_n= max(ID_running), by(date)
			 
			 replace ID_running=id_n
			 
			 
			 decode market_enc, gen(market_temp) 
			 replace market=market_temp
			 
			 drop id_n obs_per_event market_enc market_temp
			 
			 
			 *** make sample split by good days and bad days
			 
			 egen shock_Sample_temp=sum(shock),  by(date)
			 
			 gen Sample_MOSTLYPOSITIVE=1 if shock_Sample_temp>0
			 gen Sample_NEUTRAL=1 if shock_Sample_temp==0
			 gen Sample_MOSTLYNEGATIVE=1 if shock_Sample_temp<0
			 
			 foreach x in  Sample_MOSTLYPOSITIVE Sample_NEUTRAL Sample_MOSTLYNEGATIVE{
			 replace `x'=0 if `x'==.
			 }
			 
			 drop shock_Sample_temp
			 
			 
  		 
		 
		 save  "$Replicationdirectory/_Work/TEMP/FTC_SHOCKS_PANEL", replace
		 
		 
 		  
		 
		 
 **** Load Panel data (same as for liquidity)

use "$Replicationdirectory/_Work/TEMP/PANEL_incl_liquidity_ytm", clear


 
xtset market_enc day_in_sample


 gen ytm_lastavail=ytm


by market_enc: replace ytm_lastavail=ytm_lastavail[_n-1]  if ytm>=. 
										
 
by market_enc: ipolate ytm day_in_sample, gen(ytm_ipolate)

 	
	
	
 	


 


xtset market_enc date

 
		* merge news shocks 
		
				*** Financial PERIOD_TREATMENTs
				merge m:1 date market  using "$Replicationdirectory/_Work/TEMP/FTC_SHOCKS_PANEL"
				
				
				
				
 
				 
				*** additional parameters
	 
 								
 	
 							
							* positive vs. news 
						 
							
							
							 
							foreach var in							POLITICAL TRADE FINANCIAL   {
							

								gen POS_`var'=1 if NEWS_positive==1 &   NEWS_`var'==1 
								gen NEG_`var'=1 if NEWS_negative==1 &   NEWS_`var'==1 
								replace POS_`var'=0 if POS_`var'==.
								replace NEG_`var'=0 if NEG_`var'==.
								

								
							}
							
  							
				*** build stacked pannel
			 
 				
				  
				rename NEWS_positive POS_NEWS
			
				rename NEWS_negative  NEG_NEWS
						
 				
		 
		 
 
		 
			
					xtset market_enc date
					
				
 					
						forvalues k=1/ 146{
						
						
 						      	preserve
											gen EVENT_`k' =1 if ID_running==`k'  
											rangestat (count ) EVENT_`k'  , interval(day_in_sample -5 5)
 										
										   
										 
											drop if EVENT_`k'_count==0
											
											
 
  											  
 											
 											*keep if market=="`market'" 
											
											by market_enc: gen eventday=_n
											 
											foreach x in  ID_running  POS_NEWS NEG_NEWS POS_POLITICAL NEG_POLITICAL POS_TRADE NEG_TRADE POS_FINANCIAL NEG_FINANCIAL      {
											
												*** this ensures that assignments for overlapping event are not messed up
												replace `x'=0 if eventday!=6
												
												
												egen `x'_EVENT=max(`x') 
												}
												
												replace SOURCE="" if   eventday!=6
												
												sort SOURCE
												
												replace  SOURCE= SOURCE[_N]

											 
  
											drop   EVENT_`k'_count EVENT_`k'
											
											 
											save "$Replicationdirectory/_Work/TEMP/FTC_EVENT_`k'", replace
											
											 
											
									restore
 					
										 
					}	
				 
				*
				 
				 
				 
							clear
							
			 
									forvalues k=1/146{
									append using "$Replicationdirectory/_Work/TEMP/FTC_EVENT_`k'"
										
									
									}
									
	  
		 
		
		*** set panel structure

			order ID_running   eventday

			sort date

			  

			egen EVENt_ID_MARKET=group(ID_running_EVENT  market_enc)


			 xtset EVENt_ID_MARKET eventday
			 
 		
		
			**** Mark  treated markets and heterogenous news
			
			
				gen PERIOD_TREATMENT=0 if eventday<6
				replace PERIOD_TREATMENT=1 if  PERIOD_TREATMENT!=0
				
				
				foreach x in   POS_NEWS  NEG_NEWS POS_POLITICAL NEG_POLITICAL POS_TRADE NEG_TRADE POS_FINANCIAL NEG_FINANCIAL  {

					egen treatedgroup_`x'= max(`x')  , by(EVENt_ID_MARKET)	

				   
				   gen TE_`x'=PERIOD_TREATMENT*treatedgroup_`x'
				   
				   egen NT_`x'=sum(TE_`x'), by (ID_running_EVENT)
				   
				   gen HET_`x'= 1 if NT_`x'!=0 & NT_`x'!=24
				   
				   replace HET_`x'= 0 if HET_`x'==.

				}
				
				 
					****  Mark if there is any treatment in observation (positive or negative)
								
					 gen ANY_TREATEDGROUP=1 if treatedgroup_POS_NEWS==1 | treatedgroup_NEG_NEWS==1 | treatedgroup_POS_POLITICAL==1 | treatedgroup_NEG_POLITICAL==1 | treatedgroup_POS_TRADE==1 | treatedgroup_NEG_TRADE==1 | treatedgroup_POS_FINANCIAL==1 | treatedgroup_NEG_FINANCIAL==1 
					   
					 replace ANY_TREATEDGROUP=0 if ANY_TREATEDGROUP==.
					   

 			
		 	  
	
		*** export for documentation
		
			preserve
			
	

				 

							keep if eventday==6
							
							keep date market ANY_TREATEDGROUP TE_NEG_NEWS TE_POS_NEWS NEWS_FINANCIAL
							
							egen NEWSSHOCK_EVENT=max(TE_POS_NEWS), by(date)
							egen NEWSSHOCK_EVENT_neg=max(TE_NEG_NEWS), by(date)
							
					 
									
									 
							
							
							replace NEWSSHOCK_EVENT=-1 if NEWSSHOCK_EVENT_neg==1
							
							drop NEWSSHOCK_EVENT_neg TE_POS_NEWS TE_NEG_NEWS
							
							
							
							reshape wide ANY_TREATEDGROUP NEWS_FINANCIAL, i(date) j(market) string
							
							gen NEWS_FINANCIAL=0
							replace NEWS_FINANCIAL=1 if NEWS_FINANCIALamsterdam==1 | NEWS_FINANCIALlondon==1 | NEWS_FINANCIALparis==1 | NEWS_FINANCIALzurich==1 
							
							drop NEWS_FINANCIALamsterdam NEWS_FINANCIALlondon NEWS_FINANCIALparis NEWS_FINANCIALzurich
							
							sum NEWS_FINANCIAL
							
							order date ANY_TREATEDGROUPlondon ANY_TREATEDGROUPparis ANY_TREATEDGROUPamsterdam ANY_TREATEDGROUPzurich NEWSSHOCK_EVENT NEWS_FINANCIAL
							
							export excel using "$Replicationdirectory/_Work/TEMP/ALL_EVENTDAYS_FORDOC_ONLY.xlsx", firstrow(variables) replace
			restore

 	 
							  
			 
			 	 
											

			 save "$Replicationdirectory/_Work/TEMP/PANEL_ALLEVENTS_ytm", replace 
			 
			 
			 
			 
				
